package demo.jms.listener;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.springframework.jms.support.converter.MessageConverter;

import demo.entity.Email;

public class EmailQueueListener implements MessageListener {
	@Resource(name = "emailMessageConverter")
	private MessageConverter messageConverter;

	@Override
	public void onMessage(Message message) {
		if (message instanceof ObjectMessage) {
			ObjectMessage objMessage = (ObjectMessage) message;
			try {
				// System.setProperty("org.apache.activemq.SERIALIZABLE_PACKAGES","*");// 指定可反序列化的包
				// Object obj = objMessage.getObject();// 必须指定可反序列化的包，否则报错
				// Email email = (Email) obj;
				/*
				 * 上面的代码写在了转换器messageConverter的fromMessage方法中了
				 */
				Email email = (Email) messageConverter.fromMessage(objMessage);
				System.out.println("[EmailQueueListener接收到一个Email消息] "+email.toString());
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

}
